package safrain.nodelet.structure;

import java.util.HashMap;

import safrain.nodelet.exception.TypeNotRegisteredException;

public class StructureService {

	/**
	 * 如果取一个没有注册的结构，是否自动注册
	 */
	public boolean autoRegister = true;

	private final HashMap<Class<?>, Structure> structureMap = new HashMap<Class<?>, Structure>();

	public Structure register(Class<?> clazz) {
		Structure structure = structureMap.get(clazz);
		if (structure == null) {
			structure = new Structure(clazz);
			structureMap.put(clazz, structure);
		}
		return structure;
	}

	public Structure getStructure(Class<?> clazz) {
		Structure structure = structureMap.get(clazz);
		if (structure == null) {
			if (autoRegister) {
				structure = register(clazz);
			} else {
				throw new TypeNotRegisteredException(clazz);
			}
		}
		return structure;
	}
}
